package com.oscarmovie.model;

import java.util.Set;

public class Image {
	private int id;
	private String name;
	private String nameCn;
	private String type;
	private String data = "thumb.png";
	
	//one-to-many, for icon
	private Set<Movie> movies;
	private Set<Person> persons;
	
	private String displayName;
	private String url = "uploaded/images/thumb.png";
	
	public String getDisplayName() {
		if (displayName == null) {
			displayName = name;
			if (nameCn!=null && !"".equals(nameCn)) {
				displayName = nameCn + " " +name;
			}
		}
		return displayName;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getData() {
		return data;
	}
	
	public String getSrc() {
		if(data==null) data="thumb.png";
		String imagepath = "";
		if(ImageMovie.class.isInstance(this)){
			if(movies.size()>0){//大于0（实际上只会是1）：表示Movie-Image的关系是many-to-one，即，这个image是电影海报或人物头像	
				imagepath = "http://www.oscarchinese.com/uploaded/movie/thumb/" + data;
			} else {
				imagepath = "http://www.oscarchinese.com/uploaded/movie/origin/" + data;
			}
		} else if (ImagePerson.class.isInstance(this)) {
			if(persons.size()>0){
				imagepath = "http://www.oscarchinese.com/uploaded/person/thumb/" + data;
			} else {
				imagepath = "http://www.oscarchinese.com/uploaded/person/origin/" + data;
			}
		}
		return imagepath;
	}

	public void setData(String data) {
		this.data = data;
	}
	public void setNameCn(String nameCn) {
		this.nameCn = nameCn;
	}
	public String getNameCn() {
		return nameCn;
	}
	
//	public int hashCode() {
//		return this.id;
//	}
//	public boolean equals(Object object) {
//		boolean t = false;
//		if (object != null) {
//			Image image = (Image) object;
//			t = image.getId() == this.getId();
//		}
//		return t;
//	}
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!getClass().isAssignableFrom( obj.getClass()))
			return false;
		Image other = (Image) obj;
		if (getId() != other.getId())
			return false;
		return true;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUrl() {
		return url;
	}
	public void setMovies(Set<Movie> movies) {
		this.movies = movies;
	}
	public Set<Movie> getMovies() {
		return movies;
	}
	public void setPersons(Set<Person> persons) {
		this.persons = persons;
	}
	public Set<Person> getPersons() {
		return persons;
	}
	
	
	
}
